package com.guet.main.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guet.main.Listener.SliceListener;
import com.guet.main.Listener.UserListener;
import com.guet.main.entity.RoomTimeSlice;
import com.guet.main.entity.vo.QuerySliceVo;
import com.guet.main.entity.vo.SliceVo;
import com.guet.main.entity.vo.UserVo;
import com.guet.main.mapper.RoomTimeSliceMapper;
import com.guet.main.service.RoomService;
import com.guet.main.service.RoomTimeSliceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guet.main.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * room_time_slice 服务实现类
 * </p>
 *
 * @author csp
 * @since 2022-07-28
 */
@Service
public class RoomTimeSliceServiceImpl extends ServiceImpl<RoomTimeSliceMapper, RoomTimeSlice> implements RoomTimeSliceService {


    @Autowired
    private RoomTimeSliceService roomTimeSliceService;
    @Override
    public List<RoomTimeSlice> querySearch(QuerySliceVo querySliceVo) {
        String beginTime = querySliceVo.getBeginTime();
        String endTIme = querySliceVo.getEndTIme();
        String roomId = querySliceVo.getRoomId();
        QueryWrapper<RoomTimeSlice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("slice_date");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String date = format1.format(new Date());
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm");
        String time = format2.format(new Date());
        queryWrapper.or(wrapper1->wrapper1.gt("slice_date",date).or(wrapper2->wrapper2.and(wrapper3->wrapper3.eq("slice_date",date).ge("begin_time",time))));
        if(beginTime!=null&&!beginTime.equals("null")){
            queryWrapper.ge("slice_date",beginTime);
        }
        if(endTIme!=null&&!endTIme.equals("null")){
            queryWrapper.le("slice_date",endTIme);
        }
        if(roomId!=null&&!roomId.equals("null")){
            queryWrapper.eq("room_id",roomId);
        }
        queryWrapper.orderByAsc("slice_date");
        return roomTimeSliceService.list(queryWrapper);
    }

    @Override
    public void saveSlice(MultipartFile file, RoomTimeSliceService roomTimeSliceService, RoomService roomService,String roomId) {
        try {
            //得到文件的输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SliceVo.class, new SliceListener(roomTimeSliceService,roomService,roomId)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
